package org.yczbj.ycvideoplayer.clarity;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import com.yc.video.config.ConstantKeys;
import com.yc.video.tool.TextUtils;
import com.yc.video.ui.view.CustomBottomView;

import org.yczbj.ycvideoplayer.ResourceTable;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * DefinitionControlView
 *
 * @since 2021-05-12
 */
public class DefinitionControlView extends CustomBottomView {
    private Text mDefinition;
    private PopupDialog mPopupWindow;
    private List<String> mRateStr;
    private DirectionalLayout mPopLayout;
    private int mCurIndex;
    private LinkedHashMap<String, String> mMultiRateData;
    private OnRateSwitchListener mOnRateSwitchListener;
    private Component.ClickedListener rateOnClickListener = new Component.ClickedListener() {
        @Override
        public void onClick(Component v) {
            int index = (int) v.getTag();
            if (mCurIndex == index) {
                return;
            }
            ((Text) mPopLayout.getComponentAt(mCurIndex)).setTextColor(Color.BLACK);
            ((Text) mPopLayout.getComponentAt(index)).setTextColor(new Color(Color.getIntColor("#60C2CB")));
            mDefinition.setText(mRateStr.get(index));
            switchDefinition(mRateStr.get(index));
            mPopupWindow.destroy();
            mCurIndex = index;
        }
    };

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public DefinitionControlView(Context context) {
        super(context);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 自定义属性
     */
    public DefinitionControlView(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 自定义属性
     * @param defStyleAttr 默认主题风格
     */
    public DefinitionControlView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    {
        mPopupWindow = new PopupDialog(getContext(), null,
            ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        mPopLayout = (DirectionalLayout) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_layout_rate_pop, this, false);
        mPopupWindow.setCustomComponent(mPopLayout);
        mPopupWindow.setBackColor(new Color(0xffffffff));
        mDefinition = (Text) findComponentById(ResourceTable.Id_tv_definition);
        mDefinition.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showRateMenu();
            }
        });
    }

    private void showRateMenu() {
        mPopLayout.estimateSize(Component.EstimateSpec.UNCONSTRAINT, Component.EstimateSpec.UNCONSTRAINT);
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_layout_definition_control_view;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animator anim) {
        super.onVisibilityChanged(isVisible, anim);
        if (!isVisible) {
            mPopupWindow.destroy();
        }
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
        super.onPlayerStateChanged(playerState);
        if (playerState == ConstantKeys.PlayMode.MODE_FULL_SCREEN) {
            mDefinition.setVisibility(VISIBLE);
        } else {
            mDefinition.setVisibility(HIDE);
            mPopupWindow.destroy();
        }
    }

    /**
     * 设置数据
     *
     * @param multiRateData 多速率数据
     */
    public void setData(LinkedHashMap<String, String> multiRateData) {
        mMultiRateData = multiRateData;
        if (mDefinition != null && TextUtils.isEmpty(mDefinition.getText())) {
            if (multiRateData == null) {
                return;
            }
            mRateStr = new ArrayList<>();
            int index = 0;
            ListIterator<Map.Entry<String, String>> iterator = new ArrayList<>(
                multiRateData.entrySet()).listIterator(multiRateData.size());
            while (iterator.hasPrevious()) { // 反向遍历
                Map.Entry<String, String> entry = iterator.previous();
                mRateStr.add(entry.getKey());
                Text rateItem = (Text) LayoutScatter.getInstance(getContext()).parse(
                    ResourceTable.Layout_layout_rate_item, null, false);
                rateItem.setText(entry.getKey());
                rateItem.setTag(index);
                rateItem.setClickedListener(rateOnClickListener);
                mPopLayout.addComponent(rateItem);
                index++;
            }
            ((Text) mPopLayout.getComponentAt(index - 1)).setTextColor(new Color(Color.getIntColor("#60C2CB")));
            mDefinition.setText(mRateStr.get(index - 1));
            mCurIndex = index - 1;
        }
    }

    private void switchDefinition(String s) {
        mControlWrapper.hide();
        mControlWrapper.stopProgress();
        String url = mMultiRateData.get(s);
        if (mOnRateSwitchListener != null) {
            mOnRateSwitchListener.onRateChange(url);
        }
    }

    /**
     * OnRateSwitchListener
     *
     * @since 2021-05-12
     */
    public interface OnRateSwitchListener {
        /**
         * RateChange
         *
         * @param url 字符串地址
         */
        void onRateChange(String url);
    }

    public void setOnRateSwitchListener(OnRateSwitchListener onRateSwitchListener) {
        mOnRateSwitchListener = onRateSwitchListener;
    }
}
